<?php
include('./config.php');

if (!isset($admin_username))
{
	if (!file_exists('./install.php'))
	{
		die('<div style="font-family: Verdana; font-size: 12px; font-weight: bold;">CloudCMS is not installed, there is also no installation file located.</div>');
	}
	else
	{
		header('Location: ./install.php');	
	}
}

if (isset($_GET['p']))
{
	if (empty($_GET['p']))
	{
		$page_content = file_get_contents('./pages/' . $default_page);
		$page_content = str_replace(array('\\'), array(''), $page_content);
		$title_search = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
		$title_replace = array('', '', '', '', '', '', '', '', '', '');
		$page_title = str_replace($title_search, $title_replace, $default_page);
	}
	else
	{
		if (!file_exists('./pages/' . $_GET['p']))
		{
			// Check subpages:
			if (!file_exists('./pages/subpages/' . $_GET['p']))
			{
				$page_content = file_get_contents('./pages/' . $default_page);
				$page_content = str_replace(array('\\'), array(''), $page_content);
				$title_search = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
				$title_replace = array('', '', '', '', '', '', '', '', '', '');
				$page_title = str_replace($title_search, $title_replace, $default_page);
			}
			else
			{
				$page_content = file_get_contents('./pages/subpages/' . $_GET['p']);
				$page_content = str_replace(array('\\'), array(''), $page_content);
				$page_title = $_GET['p'];
			}
		}
		else
		{
			$page_content = file_get_contents('./pages/' . $_GET['p']);
			$page_content = str_replace(array('\\'), array(''), $page_content);
			$title_search = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
			$title_replace = array('', '', '', '', '', '', '', '', '', '');
			$page_title = str_replace($title_search, $title_replace, $_GET['p']);
		}
	}
}
else
{
	$page_content = file_get_contents('./pages/' . $default_page);
	$page_content = str_replace(array('\\'), array(''), $page_content);
	$title_search = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
	$title_replace = array('', '', '', '', '', '', '', '', '', '');
	$page_title = str_replace($title_search, $title_replace, $default_page);
}

$before = '&nbsp;&nbsp;';
$after = '&nbsp;&nbsp;';

if ($handle = opendir('./pages/'))
{
	while ($file = readdir($handle))
	{
		clearstatcache();
		if (is_file('./pages/' . $file))
		{
			$search = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
			$replace = array('', '', '', '', '', '', '', '', '', '');
			$newfilename = str_replace($search, $replace, $file);
			
			$result[] = $before . '<a href="index.php?p=' . $file . '">' . $newfilename . '</a>' . $after;
		}
	}
	$newresult = implode('', $result);
	closedir($handle);
}

$template = file_get_contents('./templates/' . $default_template . '/template.html');
$search = array('[[SITE_TITLE]]', '[[PAGE_TITLE]]', '[[PAGE_CONTENT]]', '[[MENU_LINKS]]', '[[DEFAULT_TEMPLATE]]');
$replace = array($site_title, $page_title, $page_content, $newresult, $default_template);
$template = str_replace($search, $replace, $template);
echo $template;
?>